package Java.view.Common;

import Java.utils.MusicPlayer;
import Java.view.game.dian21.dian21FX;
import Java.view.game.douniu.douniuFX;
import Java.view.game.douniu.douniuTeamFX;
import Java.view.game.zhajinhua.zhajinhuaFX;
import domain.User;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class GameFX extends Application {
    boolean flag = true; // 控制声音
    String grade = "", usernameText = "";
    int dou = 0, exprience = 0, max = 0;
    User user;

    Button toReturn = new Button(); // 退出登录
    Button music = new Button(); // 音乐控制
    Button setting = new Button(); // 头像
    Button jingdian = new Button(); // 斗地主
    Button jinhua = new Button(); // 炸金花
    Button dian21 = new Button(); // 21点
    Button douniu = new Button(); // 斗牛
    Label username = new Label(); // 用户名
    ProgressBar bar = new ProgressBar(); // 经验条
    Label gra = new Label(); // 等级title
    Label exp = new Label(); // 经验
    Label money = new Label(); // 欢乐豆
    Button happyDou = new Button(); // 充值

    public GameFX(User user1) {
        user = user1;
        int temp = user.getExp();
        usernameText = user.getUsername();
        if (temp < 10) { // 判断等级
            grade = "短工";
            exprience = temp;
            max = 10;
        } else if (temp < 20) {
            grade = "长工";
            exprience = temp - 10;
            max = 10;
        } else if (temp < 40) {
            grade = "佃户";
            max = 20;
            exprience = temp - 20;
        } else if (temp < 60) {
            grade = "贫农";
            exprience = temp - 40;
            max = 20;
        } else if (temp < 100) {
            grade = "渔夫";
            exprience = temp - 60;
            max = 40;
        } else if (temp < 200) {
            grade = "猎人";
            exprience = temp - 100;
            max = 100;
        } else if (temp < 400) {
            grade = "中农";
            exprience = temp - 200;
            max = 200;
        } else if (temp < 600) {
            grade = "富农";
            exprience = temp - 400;
            max = 200;
        } else if (temp < 1000) {
            grade = "掌柜";
            exprience = temp - 600;
            max = 400;
        } else if (temp < 2000) {
            grade = "商人";
            exprience = temp - 1000;
            max = 1000;
        } else if (temp < 5000) {
            grade = "衙役";
            exprience = temp - 2000;
            max = 3000;
        } else if (temp < 10000) {
            grade = "小财主";
            exprience = temp - 5000;
            max = 5000;
        } else if (temp < 20000) {
            grade = "大财主";
            exprience = temp - 10000;
            max = 10000;
        } else if (temp < 50000) {
            grade = "小地主";
            exprience = temp - 20000;
            max = 30000;
        } else if (temp < 100000) {
            grade = "大地主";
            exprience = temp - 50000;
            max = 50000;
        } else {
            grade = "知县";
            exprience = temp - 100000;
        }
        dou = user.getHappyDou();
    }

    public void start(Stage primaryStage) {
        Pane root = new Pane();
        root.setPrefSize(800, 600);
        root.setStyle("-fx-background-image: url('" + getClass().getResource("/image/主界面背景.jpg").toExternalForm() + "');");

        initView(root);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("游戏首页");
        primaryStage.getIcons().add(new Image("file:src/image/dizhu.png"));
        primaryStage.show();
    }

    private void initView(Pane root) {
        username.setFont(Font.font(null, 20));
        username.setStyle("-fx-text-fill: orange;");
        username.setText(usernameText);
        username.setLayoutX(605);
        username.setLayoutY(16);
        root.getChildren().add(username);

        gra.setFont(Font.font(null, 16));
        gra.setStyle("-fx-text-fill: black;");
        gra.setText(grade);
        gra.setLayoutX(670);
        gra.setLayoutY(11);
        root.getChildren().add(gra);

        exp.setFont(Font.font(null, 10));
        exp.setStyle("-fx-text-fill: black;");
        exp.setText(exprience + "/" + max);
        exp.setLayoutX(670);
        exp.setLayoutY(28);
        root.getChildren().add(exp);

        money.setFont(Font.font(null, 24));
        money.setStyle("-fx-text-fill: black;");
        money.setText(String.valueOf(dou));
        money.setLayoutX(370);
        money.setLayoutY(565);

        bar.setProgress(exprience / (double) max);
        bar.setLayoutX(600);
        bar.setLayoutY(43);
        bar.setPrefWidth(100);
        root.getChildren().add(bar);

        setupButton(toReturn, "file:src/image/返回.png", 15, 8);
        setupButton(music, flag ? "file:src/image/喇叭.png" : "file:src/image/静音.png", 80, 6);
        setupButton(setting, "file:src/image/dizhu.png", 720, 18);
        setupButton(jingdian, "file:src/image/login/斗地主.png", 180, 100);
        setupButton(jinhua, "file:src/image/login/炸金花.png", 440, 100);
        setupButton(dian21, "file:src/image/login/二十一点.png", 530, 300);
        setupButton(douniu, "file:src/image/login/斗牛.png", 70, 300);
        setupButton(happyDou, "file:src/image/欢乐豆.png", 275, 535);

        root.getChildren().addAll(toReturn, music, setting, jingdian, jinhua, dian21, douniu, happyDou,money);
    }

    private void setupButton(Button button, String imagePath, double x, double y) {
        button.setLayoutX(x);
        button.setLayoutY(y);
        button.setGraphic(new ImageView(new Image(imagePath)));
        button.setStyle("-fx-background-color: transparent;");
        button.setPadding(Insets.EMPTY);
        button.setOnMouseClicked(e -> {
            try {
                handleButtonClick(button);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        button.setOnMouseEntered(event -> button.setCursor(Cursor.HAND));

        // 设置鼠标退出事件，光标恢复为默认
        button.setOnMouseExited(event -> button.setCursor(Cursor.DEFAULT));
    }

    private void handleButtonClick(Button button) throws Exception {
        if (button == toReturn) {
            ((Stage) toReturn.getScene().getWindow()).close();
            new LoginFX(user).start(new Stage());

        }
        if (button == music) {
            flag = !flag;
            if (flag) {
                music.setGraphic(new ImageView(new Image("file:src/image/喇叭.png")));
                MusicPlayer.getInstance().changePlay("music/start.wav");
            } else {
                music.setGraphic(new ImageView(new Image("file:src/image/静音.png")));
                MusicPlayer.getInstance().over();
            }
        }
        if (button == happyDou) {
            showJDialog("充值欢乐豆");
        }
        if(button==jingdian){
            new teamFX(user,1).start(new Stage());
            ((Stage) jingdian.getScene().getWindow()).close();
        }
        if(button==douniu){
            new douniuTeamFX(user).start(new Stage());
            ((Stage) jingdian.getScene().getWindow()).close();
        }
        if(button==jinhua){
            new zhajinhuaFX(user).start(new Stage());
            ((Stage) jingdian.getScene().getWindow()).close();
        }
        if(button==dian21){
            new teamFX(user,4).start(new Stage());
            ((Stage) jingdian.getScene().getWindow()).close();

        }
    }

    private void showJDialog(String content) {
        Stage dialog = new Stage();
        Pane pane = new Pane();
        Label label = new Label(content);
        label.setLayoutX(40);
        label.setLayoutY(20);
        pane.getChildren().add(label);

        Scene scene = new Scene(pane, 180, 90);
        dialog.setScene(scene);
        dialog.setAlwaysOnTop(true);
        dialog.showAndWait();
    }

    public static void main(String[] args) {
        User user = new User("12","1233",888,888); // 此处初始化用户
        new GameFX(user).start(new Stage());
    }
}
